// balance

// table S1 (T0 vs. T1) 

use "output/data.dta", clear
destring id, replace
xtset id time
replace yob = 1970 if yob==.
	
foreach depvar in yob sex res main coop area extension sprec rotation manage conserv machine pest l_exp {
	
	sum `depvar' if time==0 & CTRL==1
	scalar b1`depvar' = r(mean)
	loc b1`depvar': display %4.2f b1`depvar'
	scalar se1`depvar' = r(sd)
	loc se1`depvar': display %4.2f se1`depvar'		
	scalar obs1 = r(N)
	loc obs1: display %9.0f obs1
			
	sum `depvar' if time==0 & CAP==1		
	scalar b2`depvar' = r(mean)
	loc b2`depvar': display %4.2f b2`depvar'
	scalar se2`depvar' = r(sd)
	loc se2`depvar': display %4.2f se2`depvar'
	scalar obs2 = r(N)
	loc obs2: display %9.0f obs2
	
	reg `depvar' CAP if time==0 & CAP_ATEG==0, robust
	scalar b3`depvar' = _b[CAP]
	loc b3`depvar': display %4.2f b3`depvar'
	scalar se3`depvar' = _se[CAP]
	loc se3`depvar': display %4.2f se3`depvar'
	loc t = _b[CAP]/_se[CAP]
	loc p = 2*ttail(e(df_r),abs(`t'))
*	loc star3`depvar' = cond(`p'<0.01,"***",cond(`p'<0.05,"**",cond(`p'<0.10,"*","")))	
	scalar obs3`depvar' = e(N)
	loc obs3`depvar': display %9.0f obs3`depvar'
			
}

// joint orthogonality 
		
preserve

keep if time==0 & CAP_ATEG==0

rename (yob sex res main coop area extension sprec rotation manage conserv machine pest l_exp) (y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14)

keep id CAP y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14

reshape long y, i(id) j(vars)

sureg y CAP i.vars

test CAP

scalar value = r(chi2)
loc value: display %5.3f value

scalar pvalue = r(p)
loc pvalue: display %5.3f pvalue

restore		
		
		
file open Table using "tables/tableS1.tex", write replace
		
file write Table "\begin{table}[h!]" _n 
file write Table "\centering" _n
file write Table "\small" _n
file write Table "\caption{T1 vs. T0 (Balance)}" _n
file write Table "\label{tab:balance1}" _n
file write Table "\begin{tabularx} {\linewidth} {@{} l Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z@{}}" _n
file write Table "\toprule" _n
file write Table "\toprule" _n
file write Table " & T0 & T1 & T1 - T0 & N \\" _n
file write Table "\cmidrule{2-5}" _n				
file write Table " & (1) & (2) & (3) & (4) \\" _n
file write Table "\midrule" _n
file write Table "Year of Birth & `b1yob' & `b2yob' & `b3yob'`star3yob' & `obs3yob' \\" _n
file write Table " & [`se1yob'] & [`se2yob'] & (`se3yob') & \\" _n
file write Table "Female (0/1) & `b1sex' & `b2sex' & `b3sex'`star3sex' & `obs3sex' \\" _n
file write Table " & [`se1sex'] & [`se2sex'] & (`se3sex') & \\" _n
file write Table "Lives in the farm (0/1) & `b1res' & `b2res' & `b3res'`star3res' & `obs3res' \\" _n
file write Table " & [`se1res'] & [`se2res'] & (`se3res') & \\" _n
file write Table "Main income (0/1) & `b1main' & `b2main' & `b3main'`star3main' & `obs3main' \\" _n
file write Table " & [`se1main'] & [`se2main'] & (`se3main') & \\" _n
file write Table "Cooperative (0/1) & `b1coop' & `b2coop' & `b3coop'`star3coop' & `obs3coop' \\" _n
file write Table " & [`se1coop'] & [`se2coop'] & (`se3coop') & \\" _n		
file write Table "Area (ha) & `b1area' & `b2area' & `b3area'`star3area' & `obs3area' \\" _n
file write Table " & [`se1area'] & [`se2area'] & (`se3area') & \\" _n
file write Table "Technical Assistance (0/1) & `b1extension' & `b2extension' & `b3extension'`star3extension' & `obs3extension' \\" _n
file write Table " & [`se1extension'] & [`se2extension'] & (`se3extension') & \\" _n		
file write Table "Pasture Restoration (\%) & `b1sprec' & `b2sprec' & `b3sprec'`star3sprec' & `obs3sprec' \\" _n
file write Table " & [`se1sprec'] & [`se2sprec'] & (`se3sprec') & \\" _n
file write Table "Pasture Rotation (0/1) & `b1rotation' & `b2rotation' & `b3rotation'`star3rotation' & `obs3rotation' \\" _n
file write Table " & [`se1rotation'] & [`se2rotation'] & (`se3rotation')  & \\" _n	
file write Table "Management Practices & `b1manage' & `b2manage' & `b3manage'`star3manage' & `obs3manage' \\" _n
file write Table " & [`se1manage'] & [`se2manage'] & (`se3manage') & \\" _n	
file write Table "Conservation Practices & `b1conserv' & `b2conserv' & `b3conserv'`star3conserv' & `obs3conserv' \\" _n
file write Table " & [`se1conserv'] & [`se2conserv'] & (`se3conserv') & \\" _n	
file write Table "Machine Use (0/1) & `b1machine' & `b2machine' & `b3machine'`star3machine' & `obs3machine' \\" _n
file write Table " & [`se1machine'] & [`se2machine'] & (`se3machine')  & \\" _n
file write Table "Pesticide Use (0/1) & `b1pest' & `b2pest' & `b3pest'`star3pest' & `obs3pest' \\" _n
file write Table " & [`se1pest'] & [`se2pest'] & (`se3pest') & \\" _n				
file write Table "log(Expenditures) & `b1l_exp' & `b2l_exp' & `b3l_exp'`star3l_exp' & `obs3l_exp' \\" _n
file write Table " & [`se1l_exp'] & [`se2l_exp'] & (`se3l_exp') & \\" _n
file write Table "Joint orthogonality test & & & `value' & \\" _n
file write Table "(p-value) & & & (`pvalue') & \\" _n
file write Table "\bottomrule" _n
file write Table "\bottomrule" _n
file write Table "\end{tabularx}" _n
file write Table "\begin{tablenotes}" _n
file write Table "\small" _n
file write Table "\begin{spacing}{1.0}" _n
file write Table "\item Notes: {Column 1 reports the mean of each indicator in the T0 (control) group. Column 2 reports the mean of each variable in the T1 (traininggroup. Column 3 report mean differences between the groups. Standard deviations are reported in brackets and robust standard errors are reported in parentheses. *** p<0.01; ** p<0.05; * p<0.10}" _n
file write Table "\end{spacing}" _n
file write Table "\end{tablenotes}" _n
file write Table "\end{table}%" _n
file close Table

// table S2 (T2 vs. T1)

use "output/data.dta", clear
destring id, replace
xtset id time
replace yob = 1970 if yob==.

foreach depvar in yob sex res main coop area extension sprec rotation manage conserv machine pest l_rev l_exp {
	
	sum `depvar' if time==0 & CAP==1
	scalar b1`depvar' = r(mean)
	loc b1`depvar': display %4.2f b1`depvar'
	scalar se1`depvar' = r(sd)
	loc se1`depvar': display %4.2f se1`depvar'
	scalar obs1 = r(N)
	loc obs1: display %9.0f obs1
			
	sum `depvar' if time==0 & CAP_ATEG==1
	scalar b2`depvar' = r(mean)
	loc b2`depvar': display %4.2f b2`depvar'
	scalar se2`depvar' = r(sd)
	loc se2`depvar': display %4.2f se2`depvar'		
	scalar obs2 = r(N)
	loc obs2: display %9.0f obs2
	
	reg `depvar' CAP_ATEG if time==0 & CTRL==0, robust
	scalar b3`depvar' = _b[CAP_ATEG]
	loc b3`depvar': display %4.2f b3`depvar'
	scalar se3`depvar' = _se[CAP_ATEG]
	loc se3`depvar': display %4.2f se3`depvar'
	loc t = _b[CAP_ATEG]/_se[CAP_ATEG]
	loc p = 2*ttail(e(df_r),abs(`t'))
*	loc star3`depvar' = cond(`p'<0.01,"***",cond(`p'<0.05,"**",cond(`p'<0.10,"*","")))			
	scalar obs3`depvar' = e(N)
	loc obs3`depvar': display %9.0f obs3`depvar'
			
}

// joint test

preserve

keep if time==0 & CTRL==0

rename (yob sex res main coop area extension sprec rotation manage conserv machine pest l_exp) (y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14)

keep id CAP_ATEG y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14

reshape long y, i(id) j(vars)

sureg y CAP_ATEG i.vars

test CAP_ATEG

scalar value = r(chi2)
loc value: display %5.3f value
	
scalar pvalue = r(p)
loc pvalue: display %5.3f pvalue	
	
restore 	
	
		
file open Table using "tables/tableS2.tex", write replace
		
file write Table "\begin{table}[h!]" _n 
file write Table "\centering" _n
file write Table "\small" _n
file write Table "\caption{T2 vs. T1 (balance)}" _n
file write Table "\label{tab:balance2}" _n
file write Table "\begin{tabularx} {\linewidth} {@{} l Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z@{}}" _n
file write Table "\toprule" _n
file write Table "\toprule" _n
file write Table " & T1 & T2 & T2 - T1 & N \\" _n
file write Table "\cmidrule{2-5}" _n				
file write Table " & (1) & (2) & (3) & (4) \\" _n
file write Table "\midrule" _n
file write Table "Year of Birth & `b1yob' & `b2yob' & `b3yob'`star3yob' & `obs3yob' \\" _n
file write Table " & [`se1yob'] & [`se2yob'] & (`se3yob') & \\" _n
file write Table "Female (0/1) & `b1sex' & `b2sex' & `b3sex'`star3sex' & `obs3sex' \\" _n
file write Table " & [`se1sex'] & [`se2sex'] & (`se3sex') & \\" _n
file write Table "Lives in the farm (0/1) & `b1res' & `b2res' & `b3res'`star3res' & `obs3res' \\" _n
file write Table " & [`se1res'] & [`se2res'] & (`se3res') & \\" _n
file write Table "Main income (0/1) & `b1main' & `b2main' & `b3main'`star3main' & `obs3main' \\" _n
file write Table " & [`se1main'] & [`se2main'] & (`se3main') & \\" _n
file write Table "Cooperative (0/1) & `b1coop' & `b2coop' & `b3coop'`star3coop' & `obs3coop' \\" _n
file write Table " & [`se1coop'] & [`se2coop'] & (`se3coop') & \\" _n		
file write Table "Area (ha) & `b1area' & `b2area' & `b3area'`star3area' & `obs3area' \\" _n
file write Table " & [`se1area'] & [`se2area'] & (`se3area') & \\" _n
file write Table "Technical Assistance (0/1) & `b1extension' & `b2extension' & `b3extension'`star3extension' & `obs3extension' \\" _n
file write Table " & [`se1extension'] & [`se2extension'] & (`se3extension') & \\" _n		
file write Table "Pasture Restoration (\%) & `b1sprec' & `b2sprec' & `b3sprec'`star3sprec' & `obs3sprec' \\" _n
file write Table " & [`se1sprec'] & [`se2sprec'] & (`se3sprec') & \\" _n
file write Table "Pasture Rotation (0/1) & `b1rotation' & `b2rotation' & `b3rotation'`star3rotation' & `obs3rotation' \\" _n
file write Table " & [`se1rotation'] & [`se2rotation'] & (`se3rotation')  & \\" _n	
file write Table "Management Practices & `b1manage' & `b2manage' & `b3manage'`star3manage' & `obs3manage' \\" _n
file write Table " & [`se1manage'] & [`se2manage'] & (`se3manage') & \\" _n	
file write Table "Conservation Practices & `b1conserv' & `b2conserv' & `b3conserv'`star3conserv' & `obs3conserv' \\" _n
file write Table " & [`se1conserv'] & [`se2conserv'] & (`se3conserv') & \\" _n	
file write Table "Machine Use (0/1) & `b1machine' & `b2machine' & `b3machine'`star3machine' & `obs3machine' \\" _n
file write Table " & [`se1machine'] & [`se2machine'] & (`se3machine')  & \\" _n
file write Table "Pesticide Use (0/1) & `b1pest' & `b2pest' & `b3pest'`star3pest' & `obs3pest' \\" _n
file write Table " & [`se1pest'] & [`se2pest'] & (`se3pest') & \\" _n				
file write Table "log(Expenditures) & `b1l_exp' & `b2l_exp' & `b3l_exp'`star3l_exp' & `obs3l_exp' \\" _n
file write Table " & [`se1l_exp'] & [`se2l_exp'] & (`se3l_exp') & \\" _n
file write Table "Joint orthogonality test & & & `value' & \\" _n
file write Table "(p-value) & & & (`pvalue') & \\" _n
file write Table "\bottomrule" _n
file write Table "\bottomrule" _n
file write Table "\end{tabularx}" _n
file write Table "\begin{tablenotes}" _n
file write Table "\small" _n
file write Table "\begin{spacing}{1.0}" _n
file write Table "\item Notes: {Column 1 reports the mean of each indicator in the T0 (control) group. Column 2 reports the mean of each variable in the T1 (traininggroup. Column 3 report mean differences between the groups. Standard deviations are reported in brackets and robust standard errors are reported in parentheses. *** p<0.01; ** p<0.05; * p<0.10}" _n
file write Table "\end{spacing}" _n
file write Table "\end{tablenotes}" _n
file write Table "\end{table}%" _n
file close Table		
		
